<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="false">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row :gutter="48">
          <a-col :span="12">
            <a-form-model-item label="工作单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workUnit">
              <a-input :disabled=formDisabled v-model="model.workUnit" placeholder="请输入工作单位"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="开始日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="beginDate">
              <a-date-picker :disabled="formDisabled" placeholder="请选择开始日期" valueFormat="YYYY-MM-DD" @change="handleChange" v-model="model.beginDate" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="结束日期" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="endDate">
              <a-date-picker
                :disabled="formDisabled"
                :disabled-date="disabledDate"
                :default-picker-value="this.model.beginDate"
                placeholder="请选择结束日期"
                valueFormat="YYYY-MM-DD"
                v-model="model.endDate"
                style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="所属部门" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="department">
              <a-input :disabled=formDisabled v-model="model.department" placeholder="请输入所属部门"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="岗位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="position">
              <a-input :disabled=formDisabled v-model="model.position" placeholder="请输入岗位"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="证明人" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="witness">
              <a-input :disabled=formDisabled v-model="model.witness" placeholder="请输入证明人"></a-input>
            </a-form-model-item>
          </a-col>
           <a-col :span="12">
            <a-form-model-item label="联系方式" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="witnessPhone">
              <a-input :disabled=formDisabled v-model="model.witnessPhone" placeholder="请输入联系方式"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="性质" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="nature">
              <a-input :disabled=formDisabled v-model="model.nature" placeholder="请输入性质"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="薪资（元)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="salary">
              <a-input-number :disabled=formDisabled :max="99999999" :min="0"  v-model="model.salary" placeholder="请输入工资" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="12">
            <a-form-model-item label="离职原因" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="leaveReason">
              <a-input :disabled=formDisabled v-model="model.leaveReason" placeholder="请输入离职原因"  ></a-input>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import moment from 'moment';
export default {
  name: 'ErpEmployeeWorkHistoryForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false
    }
  },
  data() {
    return {
      model: {},
      labelCol: {},
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 },
      },
      confirmLoading: false,
      validatorRules: {
        workUnit: [{ required: true, message: '请输入工作单位!', trigger: 'blur' }],
        position: [{ required: true, message: '请输入岗位!', trigger: 'blur' }],
        witnessPhone: [{ pattern: /^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\d{8}$/, message: '请输入正确手机号', trigger: 'blur' }],
        salary: [{ pattern: /^[1-9]\d*$/, message: '请输入正确工资', trigger: 'blur' }],
      },
      url: {
        add: '/api/erp/files/employee/erpEmployeeWorkHistory/add',
        edit: '/api/erp/files/employee/erpEmployeeWorkHistory/edit',
        queryById: '/api/erp/files/employee/erpEmployeeWorkHistory/queryById'
      }
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    moment,
    handleChange(value) {
      if (this.model.endDate) {
        if ((this.model.endDate).diff((this.model.beginDate), 'days') < 0) {
          this.model.endDate = this.model.beginDate
        }
      }
    },
    disabledDate(current) {
      return current < moment(this.model.beginDate).subtract('day')
    },
    add() {
      this.edit(this.modelDefault);
    },
    edit(record) {
      this.$refs.form.clearValidate()
      this.model = Object.assign({}, record);
      this.visible = true;
    },
    submitForm() {
      const that = this;
      // 触发表单验证
      this.$refs.form.validate(valid => {
        if (valid) {
          // that.confirmLoading = true;
          // let httpurl = '';
          // let method = '';
          // if (!this.model.id) {
          //   httpurl += this.url.add;
          //   method = 'post';
          // } else {
          //   httpurl += this.url.edit;
          //   method = 'put';
          // }
          // httpAction(httpurl, this.model, method).then((res) => {
          //   if (res.success) {
          //     that.$message.success(res.message);
          //     that.$emit('ok');
          //   } else {
          //     that.$message.warning(res.message);
          //   }
          // }).finally(() => {
          //   that.confirmLoading = false;
          // })
          that.$emit('ok',that.model);
        }

      })
    },
  }
}
</script>
